package com.lifeng.controller;

import com.lifeng.beanprocess.AbstractPayType;
import com.lifeng.beanprocess.MyBeanPostProcessor;
import com.lifeng.domain.UserBean;
import com.lifeng.springevent.SubmitOrderEvent;
import com.lifeng.springevent.SubmitOrderEvent1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;


/**
 * Created by lifeng on 2019/4/30.
 */
@RestController
public class EventController {
    private static final Logger log = LoggerFactory.getLogger(EventController.class);
    @Resource
    private MyBeanPostProcessor myBeanPostProcessor;

    @Autowired
    private ApplicationEventPublisher eventPublisher;
    /**
     * 测试策略设计模式
     * */
    @RequestMapping("/test")
    @ResponseBody
    public Object test(String payType){
        AbstractPayType abstractPayType = myBeanPostProcessor.getPayType(payType);
        abstractPayType.pay();
        return "";
    }

    /**
     * 测试spring 发布事件
     * */
    @RequestMapping("/publish")
    @ResponseBody
    public Object publish(String id){
        SubmitOrderEvent event = new SubmitOrderEvent();
        event.setId(id);
        UserBean userBean = new UserBean();
        userBean.setId(12);
        userBean.setPassword(UUID.randomUUID().toString());
        event.setUser(userBean);
        eventPublisher.publishEvent(event);
        
        SubmitOrderEvent1 event1 = new SubmitOrderEvent1("123");
        eventPublisher.publishEvent(event1);
        log.info("订单提交事件发布结束");
        return "ok";
    }
}
